const cloud = require('wx-server-sdk')
cloud.init({env: "cloud-7gfie62x366c0976"})
const db = cloud.database()
var request = require('request-promise');
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  var res = ""
  if (event.type == "login") {
    res = await db.collection('user').where({openid: wxContext.OPENID}).get()
    var data = { database:null, userInfo:null}
    if (res.data.length == 0) {//库里没该用户
      res = await db.collection("user").add({ data:{openid:wxContext.OPENID}})
      data.database = {openId:wxContext.OPENID,_id:res._id}
    } else {
      data.database = {openId:wxContext.OPENID,_id:res.data[0]._id}
      data.userInfo = res.data[0].userInfo
    }
     res = data
  } if (event.type == "update") {
    res = await db.collection(event.table).where(event.where).update(event.update)
  } if (event.type == "get") {
    res = await db.collection(event.table).where(event.where).get()
  }if (event.type == "getPosition") {
    var a = JSON.parse(await request({
      url: "https://apis.map.qq.com/ws/geocoder/v1/?location=" + event.position + "&key=6ULBZ-7YUL4-VXXUG-XCBUI-PLKVE-K3FO2&get_poi=1"
    }))
    res = a.result.formatted_addresses.rough
  } if (event.type == "addComment") {
    db.collection("comment").add({ data: event.data }).then(res=>{
      db.collection("comment").where({ postId: event.data.postId }).count().then(res => {
        db.collection("post").doc(event.data.postId).update({ data: { comments: res.total } })
      })
    })
  }if (event.type == "post") { //发送前，在客户端先上传图片集合
    res = await db.collection("post").add({data: event.data})
  } if (event.type == "getPost") {
    if (event.where.type == "全部") {
      res = await db.collection("post").orderBy('updateTime', 'desc').skip(event.page*10).limit(10).get()
    } else {
      res = await db.collection("post").orderBy('updateTime', 'desc').where(event.where).skip(event.page * 10).limit(10).get()
    }
  }if (event.type == "deletePost") {
    res = await db.collection("post").where({
      _id: event.id
    }).get()
    cloud.deleteFile({
      fileList: res.data[0].imgs
    })
    res = await db.collection("comment").where({
      postId: event.id
    }).remove()
    res = await db.collection("post").where({
      _id: event.id
    }).remove()
  }if (event.type == "deleteComment") {
    var postid = ''
    //只有commitId的话就要先获取postid
    db.collection("comment").where({ _id: event.id }).get().then(result => {
      postid = result.data[0].postId
      db.collection("comment").where({ postId: postid }).count().then(result => {
        var count = result.total-1
        db.collection("post").doc(postid).update({ data: { comments: count } })
        db.collection("comment").where({ _id: event.id }).remove()
      })
    })
  } if (event.type == "getComment") {
    res = await db.collection("comment").orderBy('updateTime', 'desc').where(event.where).skip(event.page * 10).limit(10).get()
  } if (event.type == "increaseLook") {
    res = await db.collection("post").where(event.where).update({ data: { look: db.command.inc(1) } })
  } if (event.type == "decreaseLook") {
    res = await db.collection("post").where(event.where).update({ data: { look: db.command.inc(-1) } })
  } if (event.type == "increaseLike") {
    res = await db.collection("post").where(event.where).update({ data: { like: db.command.inc(1) } })
  } if (event.type == "decreaseLike") {
    res = await db.collection("post").where(event.where).update({ data: { like: db.command.inc(-1) } })
  }if (event.type == "count") {
    res = await db.collection(event.table).where(event.where).count()
  }return {event,openid: wxContext.OPENID,appid: wxContext.APPID,unionid: wxContext.UNIONID,env: wxContext.ENV,user: res}
}